package com.cj.framework.util;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;


import com.cj.framework.app.BaseApplication;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by Administrator on 2016/7/20.
 */
public class OkHttpImp implements IOkHttp{
    private OkHttpClient okHttpClient;
    private Handler mHandler;

    public OkHttpImp(){
        mHandler=new Handler(Looper.getMainLooper());
        okHttpClient= BaseApplication.getOkHttpClient();
    }

    @Override
    public void httpInfoGet(String url, IHttpCallback callback) {
        httpInfoGet(url,callback,0);
    }

    @Override
    public void httpInfoGet(String url, final IHttpCallback callback, final int index) {
        Request request=new Request.Builder()
                .url(url)
                .get()
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, final IOException e) {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        callback.onError(e);
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final Msg msg=new Msg.Builder().content(response.body().string()).build();
                response.close();
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                       callback.onResponse(msg,index);
                    }
                });
            }
        });
    }

    @Override
    public void httpInfoPost(String url, HashMap<String, String> data, IHttpCallback callback) {
        httpInfoPost(url,data,callback,0);
    }

    @Override
    public void httpInfoPost(String url, HashMap<String, String> data, final IHttpCallback callback, final int index) {
        FormBody.Builder builder=new FormBody.Builder();
        Iterator iterator=data.entrySet().iterator();
        while(iterator.hasNext()){
            Map.Entry entry= (Map.Entry) iterator.next();
            String key= (String) entry.getKey();
            String value= (String) entry.getValue();
            builder.add(key,value);
        }
        FormBody body=builder.build();
        Request request=new Request.Builder()
                .post(body)
                .url(url)
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, final IOException e) {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        callback.onError(e);
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final Msg msg=new Msg.Builder()
                        .content(response.body().string())
                        .build();
                response.close();
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        callback.onResponse(msg,index);
                    }
                });
            }
        });
    }
    public void httpImageDownload(String url, final String filePath, final IHttpBitmapCallback callback) {

        Request request=new Request.Builder()

                .url(url)

                .get()

                .build();

        okHttpClient.newCall(request).enqueue(new Callback() {

            @Override

            public void onFailure(Call call, IOException e) {

                Log.d("xx","OKHttpImp httpImageDownload failed");

            }



            @Override

            public void onResponse(Call call, Response response) throws IOException {

                InputStream is=response.body().byteStream();

                Bitmap bitmap= BitmapFactory.decodeStream(is);

                FileUtils.saveBitmap(filePath,bitmap);

                callback.onResponse();
            }

        });

    }


}
